<template>
  <div class="dashboard-container">
    <div class="app-container">
      <head-tool>
        <template slot="after">
          <el-button @click="showAdd('0', 1)" type="primary"
            >添加权限</el-button
          >
        </template>
      </head-tool>

      <!-- 下面的表格 -->
      <!-- 如果表格要显示树形数据，必须加 row-key -->
      <!-- default-expand-all： 默认展开所有子节点 -->
      <!-- tree-props：
              需要绑定一个对象，对象用来指定子节点的属性 
              hasChildren：代表判断是否有子节点的属性
      -->

      <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        border
        default-expand-all
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="code" label="标识"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              v-if="row.type == 1"
              @click="showAdd(row.id, 2)"
              type="text"
              >添加</el-button
            >
            <el-button @click="doEdit(row.id)" type="text">编辑</el-button>
            <el-button @click="doDel(row.id)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog :title="title" :visible.sync="showDialog">
        <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
          <el-form-item prop="name" label="权限名称">
            <el-input v-model="formData.name"></el-input>
          </el-form-item>
          <el-form-item prop="code" label="权限标识">
            <el-input v-model="formData.code"></el-input>
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description"></el-input>
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </el-form-item>
          <el-row type="flex" justify="center">
            <el-button @click="save" size="mini" type="primary">确定</el-button>
            <el-button size="mini">取消</el-button>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getPermissionList,
  getPermissionDetail,
  addPermission,
  updatePermission,
  removePermission,
} from "@/api/permission";

import { transToTreeData } from "@/utils";

export default {
  name: "permission",
  data() {
    return {
      tableData: [],
      showDialog: false,
      formData: {},
      rules: {},
    };
  },

  created() {
    this.getPermissionList();
  },

  methods: {
    // 获取所有权限列表
    async getPermissionList() {
      let res = await getPermissionList();

      // 注意：返回的res.data是列表数据，要递归转成树形数据
      this.tableData = transToTreeData(res.data, "0");
    },

    // 添加的点击事件
    showAdd(pid, type) {
      // 如果是1级，pid为'0',type为1
      // 如果是2级，pid为当前这行权限的id，type为2
      // 把传递过来的值赋值给表单绑定的对象
      // 到时候就可以整体传递给接口
      this.formData.pid = pid;
      this.formData.type = type;
      this.showDialog = true;
    },

    // 编辑的点击事件
    async doEdit(id) {
      let res = await getPermissionDetail(id);
      this.formData = res.data;
      this.showDialog = true;
    },

    async doDel(id) {
      // 发请求
      await removePermission(id)
      this.$message.success('删除成功')
      this.getPermissionList()
    },

    // 对话框确定的点击事件
    async save() {
      if (this.formData.id) {
        // 做编辑
        await updatePermission(this.formData)
      } else {
        // 调用接口发请求
        await addPermission(this.formData);
      }

      // 弹出提示
      this.$message.success(this.title + "成功！");
      // 刷新列表
      this.getPermissionList();
      // 隐藏对话框
      this.showDialog = false;
    },
  },

  computed: {
    title() {
      return this.formData.id ? "编辑权限" : "新建权限";
    },
  },
};
</script>

<style>
</style>
